/**
 * @fileoverview A collection of markers that can be added to the map and share
 * an info window.
 * @author sraub@google.com (Susannah Raub)
 */

/**
 * A collection of markers which share a set of options.  Each marker in the
 * collection can have its own position, title, and associated info window
 * content.
 * @param {google.maps.InfoWindow} opt_infowindow An infowindow to open on
 *     marker click.
 */
function MarkerCollection(opt_infowindow) {
  /**
   * @type google.maps.InfoWindow
   */
  this.infowindow_ = opt_infowindow;

  /**
   * @type Array.<google.maps.Marker>
   */
  this.markers_ = [];

  /**
   * @type Array.<google.maps.MarkerOptions>
   */
  this.markerOptions_ = [];
}

/**
 * Adds a marker to the MarkerCollection.
 * @param {LatLng} position  The position at which to display this marker.
 * @param {string} opt_title  The tooltip text to display on this marker.
 * @param {string} opt_content  The content to display in the associated info
 *     window.
 */
MarkerCollection.prototype.addMarker =
    function(position, opt_title, opt_content) {
  var marker = new google.maps.Marker(this.commonOptions_);
  marker.setPosition(position);
  marker.setMap(this.map_);
  if (opt_title) marker.setTitle(opt_title);

  var infowindow = this.infowindow_;
  var markerIndex = this.markers_.length;
  var that = this;
  marker.clickListener =
      google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(
        '<form id="editMarker">' +
        '<label>' +
          'Title: <input id="edit-marker-title"><br>' +
        '</label>' +
        '<label>' +
          'Info Window content (HTML): <input id="edit-marker-content">' +
        '</label><br>' +
        '<label>' +
          'Address/LatLng: <input id="edit-marker-address">' +
        '</label><br>' +
        '<input type="submit" value="Update">' +
        '</form>'
        );
    infowindow.open(marker.getMap(), marker);
    that.setupMarkerEdit(markerIndex);
  });

  this.markers_.push(marker);
  this.markerOptions_.push({
    position: position,
    title: opt_title,
    content: opt_content
  });
};

MarkerCollection.prototype.setupMarkerEdit = function(markerIndex) {
  var that = this;
  var markerOptions = that.markerOptions_[markerIndex];
  $('#edit-marker-address').val(markerOptions.position);
  $('#edit-marker-title').val(markerOptions.title)
  $('#edit-marker-content').val(markerOptions.content);

  that.edit = $('#editMarker').submit(function(e) {
    wizard.geocoder.geocode({
      address: $('#edit-marker-address').val()
    }, function(result, status) {
      if (status != google.maps.GeocoderStatus.OK) {
        alert('Could not understand marker address. Status: ' + status);
      } else {
        var position = result[0].geometry.location;
        markerOptions = {
          position: position,
          title: $('#edit-marker-title').val(),
          content: $('#edit-marker-content').val()
        };
        that.markerOptions_[markerIndex] = markerOptions;
        that.markers_[markerIndex].setPosition(position);
      }
    });
    return e.preventDefault();
  });
};

/**
 * Sets the map of every overlay in the collection of overlays.
 * @param {google.maps.Map} map  The map on which to render the overlays in this
 *     collection.  If map is null, the overlays are removed from the map.
 */
MarkerCollection.prototype.setMap = function(map, opt_removeListener) {
  this.map_ = map;
  for (var i = 0; i < this.markers_.length; ++i) {
    var marker = this.markers_[i];
    marker.setMap(map);
  }
};

/**
 * Extend a LatLngBounds to contain all the markers in this collection.
 * @param {google.maps.LatLngBounds} bounds  The bounds to extend.
 */
MarkerCollection.prototype.extendBounds = function(bounds) {
  for (var i = 0; i < this.markers_.length; ++i) {
    bounds.extend(this.markers_[i].getPosition());
  }
};

/**
 * @param {number} index The index of the marker to get specific options for.
 * @return {google.maps.MarkerOptions}
 */
MarkerCollection.prototype.getMarkerOptions = function(index) {
  return this.markerOptions_[index];
};

/**
 * @return {google.maps.MarkerOptions}
 */
MarkerCollection.prototype.getOptions = function() {
  return this.commonOptions_;
};

MarkerCollection.prototype.setOptions = function(options) {
  for (var i = 0; i < this.markers_.length; ++i) {
    this.markers_[i].setOptions(options);
  }
  this.commonOptions_ = options;
};

/**
 * @return {number} The number of markers in this collection.
 */
MarkerCollection.prototype.getNumMarkers = function() {
  return this.markers_.length;
};

 /**
 * @return {Array.<Marker>} Return the markers in this collection.
 */
MarkerCollection.prototype.getMarkers = function() {
  return this.markers_;
};

/**
 * Deletes markers from this collection
 */
MarkerCollection.prototype.deleteSelf = function() {
  this.setMap(null);
  delete this.markers_;
  delete this.markerOptions_;
};
